package com.tripspins.app.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.tripspins.app.R;
import com.tripspins.app.adapter.PinListAdapter;
import com.tripspins.app.bo.Trip;
import com.tripspins.app.helper.TRIP_STATUS;
import com.tripspins.app.interfaces.OnFragmentInteractionListener;

public class ListPinsFragment extends Fragment {
    private Trip trip;
    private static final String ARG_TRIP = "arg_trip";

    private OnFragmentInteractionListener mListener;
    public static ListPinsFragment newInstance(Trip trip) {
        ListPinsFragment fragment = new ListPinsFragment();
        Bundle args = new Bundle();
        args.putParcelable(ARG_TRIP, trip);
        fragment.setArguments(args);
        return fragment;
    }
    public ListPinsFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            trip = getArguments().getParcelable(ARG_TRIP);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View viewRoot = inflater.inflate(R.layout.fragment_list_pins, container, false);
        ListView listView = (ListView)viewRoot.findViewById(R.id.list_pins);
        PinListAdapter adapter = new PinListAdapter(getActivity(),trip, (OnFragmentInteractionListener)getActivity());
        try {
            listView.setAdapter(adapter);
        }catch(Exception e1){
            e1.printStackTrace();
            System.gc();
            try{
                listView.setAdapter(adapter);
            }catch (Exception e2){e2.printStackTrace();}
        }
        return viewRoot;
    }

    @Override
    public void onResume() {
        super.onResume();
        loadTopMenu();

    }

    private void loadTopMenu() {
        //getActivity().getActionBar().setCustomView(null);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mListener = (OnFragmentInteractionListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }
    @Override
    public void onPrepareOptionsMenu(Menu menu) {
        super.onPrepareOptionsMenu(menu);
        menu.clear();
        loadTopMenu();
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater menuInflater) {
        menu.clear();
        loadTopMenu();
    }


    @Override
    public void onDestroyView() {
        super.onDestroy();
        getActivity().invalidateOptionsMenu();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        getActivity().invalidateOptionsMenu();
    }


}
